/**
 * UserController
 *
 * @description :: Server-side actions for handling incoming requests.
 * @help        :: See https://sailsjs.com/docs/concepts/actions
 */

 const BlockChainApp = require("../services/BlockChainApp");
const CaseReportContract = require("../services/contracts/CaseReportContract");
 const { totalUser } = require("../services/contracts/UserContract");
 const UserContract = require("../services/contracts/UserContract");
 const Message = require("../services/Message");
 
 // const Message = require("../services/Message")
 
 module.exports = {
   // 新增病历记录
   createRecord: async function(req, res){
        var param = req.body.data
        console.log("new record: ", param)
        // var instance = await BlockChainApp.contracts.CaseRecords.deployed()
        CaseReportContract.addCaseReport(param)
        .then(
            data=>{
                return res.json(Message.success("新增病历记录完成"))
            }
        )
        .catch(
            err=>{
                console.log(err)
                return res.json(Message.error("新增病历信息错误"))
            }
        )
    },

    // 查看所有病历记录
    getAllCaseRecord: async function(req, res){
        // var instance = await BlockChainApp.contracts.CaseRecords.deployed()

        CaseReportContract.getAllCaseRecords()
        .then(
            data=>{
                return res.json(Message.success(data))
            }
        )
        .catch(
            err=>{
                return res.json(Message.error("获取病历信息记录出错"))
            }
        )
    },

    // 条件查询病历信息
    getCaseRecordWithCondition: async function(req, res){
        var param = req.body.data
        CaseReportContract.findCaseRecordWithCondition(param)
        .then(data=>{
            return res.json(Message.success(data))
        })
        .catch(error=>{
            return res.json(Message.error("获取病历信息记录出错"))
        })
    },

    // 更新病历信息
    updateCaseRecord: async function(req, res){
        var param = req.body.data
        console.log("更新病历信息：", param)
        CaseReportContract.updateCaseRecord(param)
        .then(data=>{
            console.log("病历更新完成：", data)
            return res.json(Message.success(data))
        })
        .catch(error=>{
            console.log("error: ", error)
            return res.json(Message.error("更新病历数据错误"))
        })
    },

 };
 
 